package com.gamebase.net.rpc
{
	import flash.system.Capabilities;
	
	
	public class Debug
	{
		static private const DEBUG:Boolean=true;
		static private var stack:String;
		static private var listener:Array = new Array();
				
		static public function append(... args):void
		{			
			if (!DEBUG)
				return ;
			if(args==null)
				return;
//			if(!Session.getInstance().hasEventListener(Session.E_SYSTEMERROR))			
//				Session.getInstance().addEventListener(Session.E_SYSTEMERROR, onSessionError);	
			
			if (Capabilities.isDebugger)
			{
				stack=new Error().getStackTrace();
				var output:String;
				var h:String=(new Date().hours.toString().length == 1) ? "0" + new Date().hours.toString() : new Date().hours.toString();
				var m:String=(new Date().minutes.toString().length == 1) ? "0" + new Date().minutes.toString() : new Date().minutes.toString();
				var s:String=(new Date().seconds.toString().length == 1) ? "0" + new Date().seconds.toString() : new Date().seconds.toString();
				var time:String=h + ":" + m + ":" + s;
				
				var functionName:String=(stack.split("at ")[2].split("()")[0] + '()').replace('/', ".");
				var lineNumber:String=stack.match(/(?<=:)[0-9]*(?=])/g)[1];
				
				var str:String="";
				for(var i:int=0; i < args.length; i++)
				{
					str+=args[i].toString();
				}
				
				output=functionName + 'line:' + lineNumber + ">" + str +"   @" + time;;				
				trace(output);
				if(listener.length!=0)
				{
					for each (var f:Function in listener)
					{
						f.call(null,output);
					}
				}
			}
			else
			{
				if(listener.length!=0)
				{
					for each (var fun:Function in listener)
					{
						fun.call(null,"Requires the debug version flash player.");
					}
				}
			}
		}
		
		static public function report(fun:Function):void
		{
			listener.push(fun);
		}
		
		static public function onSessionError(request:Request, header:PacketHeader, result:MethodResult):void
		{
			var string:String="ERROR CODE:" + result.errorCode + " SERVER TYPE:" + header.sid + "METHOD ID: " + header.mid;
			trace(string);
		}
		
		static public function messagePrint(serverType:int, methodID:int):void
		{
			Session.instance.addType(serverType, methodID);
		}
		
		static private function Log(obj:Object):void
		{
			
		}
	}
}